package com.qingfeng.medical.controller.front.order;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qingfeng.medical.base.controller.BaseController;
import com.qingfeng.medical.base.log.annotation.Log;
import com.qingfeng.medical.base.log.enums.BusinessType;
import com.qingfeng.medical.base.result.Result;
import com.qingfeng.medical.base.ro.EnumsRo;
import com.qingfeng.medical.biz.order.domain.entity.OrderInfo;
import com.qingfeng.medical.biz.order.domain.enums.OrderStatusEnum;
import com.qingfeng.medical.biz.order.service.OrderInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.text.ParseException;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;


/**
 * 订单表
 *
 * @author 清风学Java
 * @date 2023-04-28 17:10:43
 */
@Api(tags = "订单表管理")
@RestController
@RequestMapping("/api/info/orderInfo")
@CrossOrigin
@Validated
public class OrderInfoApiController extends BaseController {

    @Autowired
    private OrderInfoService orderInfoService;

    @ApiOperation(value = "订单表分页列表", notes = "订单表分页列表")
    @GetMapping("/list")
    @Log(title = "预约订单管理", businessType = BusinessType.SELECT, message = "订单表分页列表")
    public Result<IPage<OrderInfo>> orderInfoList(
            @ApiParam(name = "page", value = "当前页码", required = true)
            @RequestParam("page") Long page,
            @ApiParam(name = "limit", value = "每页记录数", required = true)
            @RequestParam("limit") Long limit,
            @ApiParam(name = "patientId", value = "就诊人Id", required = false)
            @RequestParam("patientId") Long patientId,
            @ApiParam(name = "orderStatus", value = "订单状态", required = false)
            @RequestParam("orderStatus") OrderStatusEnum orderStatus,
            @ApiParam(name = "userId", value = "用户Id", required = false)
            @RequestParam("userId") Long userId) {
        return Result.ok(orderInfoService.orderInfoList(new Page<OrderInfo>(page, limit), patientId, orderStatus, userId));
    }

    @ApiOperation(value = "保存订单表信息", notes = "保存订单表信息")
    @PostMapping("/save/{scheduleId}/{patientId}/{userId}")
    @Log(title = "预约订单管理", businessType = BusinessType.INSERT, message = "保存订单表信息")
    public Result<Long> saveOrders(
            @PathVariable("scheduleId") Long scheduleId,
            @PathVariable("patientId") Long patientId,
            @PathVariable("userId") Long userId) {
        return Result.ok(orderInfoService.saveOrders(scheduleId, patientId, userId));
    }

    @ApiOperation(value = "根据Id查询订单信息", notes = "根据Id查询订单信息")
    @GetMapping("/info/{id}")
    @Log(title = "预约订单管理", businessType = BusinessType.SELECT, message = "根据Id查询订单信息")
    public Result<OrderInfo> orderInfo(@PathVariable("id") Long id) {
        return Result.ok(orderInfoService.getById(id));
    }

    @ApiOperation(value = "取消预约", notes = "取消预约")
    @PutMapping("/cancelOrder/{id}")
    @Log(title = "预约订单管理", businessType = BusinessType.UPDATE, message = "取消预约")
    public Result cancelOrder(@PathVariable("id") Long id) throws ParseException {
        orderInfoService.cancelOrder(id);
        return Result.ok();
    }

    @ApiOperation(value = "返回订单状态列表", notes = "返回订单状态列表")
    @GetMapping("/status")
    @Log(title = "预约订单管理", businessType = BusinessType.SELECT, message = "返回订单状态列表")
    public Result<List<EnumsRo>> orderStatusList() {
        return Result.ok(
                Arrays.stream(OrderStatusEnum.values())
                        .map(o -> EnumsRo.builder()
                                .label(o.getMsg())
                                .value(o.name())
                                .build()
                        )
                        .collect(Collectors.toList())
        );
    }


    /*@ApiOperation(value = "删除订单表信息", notes = "删除订单表信息")
    @DeleteMapping("/delete")
    @Log(title = "订单表", businessType = BusinessType.SELECT, message = "删除订单表信息")
    public Result delete(@RequestBody Long[] ids) {
        orderInfoService.removeByIds(Arrays.asList(ids));

        return Result.ok();
    }*/

}
